import { Ast, createRootAst } from "./ast";
import { BaseVisitor } from "./core";
export interface IsContext {
    is: '1' | '0' |'2'
}
export class IsLivingVisitor extends BaseVisitor {
    visitButton(node: Ast, ctx: IsContext) {
        if (node.id.endsWith(`action`) && node.text === `进入直播间`) {
            ctx.is = `1`
        }
        // 意外情况 主播下播
        if (node.id.endsWith(`action`) && node.text === `暂未直播，去个人主页`) {
            ctx.is = `2`
        }
    }
    until(ctx: IsContext): boolean {
        return ctx.is !== '0'
    }
}


export function isLiving(max: number = 60 * 3) {
    let ctx: IsContext = {
        is: '0'
    };
    const root = createRootAst()
    const visitor = new IsLivingVisitor()
    root.visit(visitor, ctx)
    while (!visitor.until(ctx) && max > 0) {
        const root = createRootAst()
        root.visit(visitor, ctx)
        max -= 1;
        sleep(1000)
    }
    return ctx.is === `1`;
}
